有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法解析hibernate。cfg。脱机时使用xml

每当我断开与internet的连接时,我都会遇到以下异常:

org.hibernate.HibernateException: Could not parse configuration: com/mashlife/resources/hibernate.cfg.xml
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1542)
    at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:1035)
    at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:64)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1476)
    at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:1017)

Caused by: org.dom4j.DocumentException: www.hibernate.org Nested exception: www.hibernate.org
    at org.dom4j.io.SAXReader.read(SAXReader.java:484)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1532)
    ... 45 more

仅在我脱机时发生。hibernate在解析配置时是否尝试读取DTD?根本原因是什么

这是我的冬眠。cfg。xml:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/foo</property>
        <property name="connection.username">user</property>
        <property name="connection.password">pass</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- DO NOT Echo all executed SQL to stdout -->
        <property name="show_sql">false</property>

        <!-- Names the annotated entity class -->
        <!--<mapping class="org.hibernate.tutorial.annotations.Event"/>-->

    </session-factory>

</hibernate-configuration>

共 (6) 个答案

  1. # 2 楼答案

    Hibernate可以在本地解析DTD(无需网络连接)

    您的DOCTYPE正在为Hibernate 3.6使用新的名称空间(http://www.hibernate.org/dtd/),因此您的类路径中可能有旧版本的Hibernate库

    升级到Hibernate 3.6.8后,我遇到了同样的问题。最终的我有多个版本的hibernate3。类路径上的jar导致加载DTD Entity Resolver的旧的不兼容版本,该版本仅适用于旧的命名空间(http://hibernate.sourceforge.net/)。为了便于参考,这里有一个到较新的DTD Entity Resolver的链接

    我使用的是hibernate3 maven插件,它对旧版本的Hibernate有一个可传递的依赖关系,所以我只需要在hibernate3.6.8上指定一个插件依赖关系。决赛

    <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>hibernate3-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        ...
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.6.8.Final</version>
        </dependency>
    </dependencies>
    </plugin>
    
  2. # 3 楼答案

    • 另一种方法是下载DTD文件并设置文件路径。 并在java构建路径(eclipse)中设置dtd文件位置

    休眠配置

    原始DTD

    <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    

    校正的DTD

    <!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://localhost:8080/YourProject/DTDLocation/hibernate-configuration-3.0.dtd">
    

    休眠映射

    原始DTD

    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    

    校正的DTD

    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://localhost:8080/YourProject/DTDLocation/hibernate-mapping-3.0.dtd">
    
  3. # 4 楼答案

    如果这对其他人有帮助。。。我的问题是我包含了错误的Maven工件。我包括spring-hibernate3

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-hibernate3</artifactId>
            <version>2.0.8</version>
        </dependency>
    

    将其替换为spring-orm修复了此问题:

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>2.5.6.SEC03</version>
        </dependency>
    
  4. # 5 楼答案

    这是不可能的,因为hibernate jar文件也加载了一些dtd内容,但它的工作速度较慢

    (1)休眠配置文件位置

    第一个解决方案是使用类路径在系统中提供DTD文件位置。因此脱机工作的DocType是

    <!DOCTYPE hibernate-configuration SYSTEM 
        "classpath://org/hibernate/hibernate-configuration-3.0.dtd">
    

    (2)将SourceForge DTD URL与系统一起使用

    我发现另一个可行的解决方案是将DTD URL更改为SourceForge,并将声明从PUBLIC更改为SYSTEM

    因此,如果您的系统处于脱机状态,下面的方法也可以使用

    <!DOCTYPE hibernate-configuration SYSTEM 
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    

    Hibernate Work Offline

  5. # 6 楼答案

    在我的情况下: JBossAS7

    我检查:

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
    

    并排除pom中的dom4j。xml

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.1.9-SNAPSHOT</version>
            <exclusions>
                ...........
                <exclusion>
                    <artifactId>dom4j</artifactId>
                    <groupId>dom4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>